package com.base.cn.platform.os.controller.agent.user;

import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.md5.MD5;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.agent.user.AgentUserService;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.manage.user.account.CusUserCashAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 用户推广中心
 *
 * @author s.li
 * @create 2018-09-26-17:29
 */
@RestController
@RequestMapping("/api/agent/user")
public class ApiAgentUserController extends ApiBaseController {
    @Autowired
    private CommonConstants commonConstants;
    @Autowired
    private CourseService courseService;
    @Autowired
    private AgentUserService agentUserService;
    @Autowired
    private CusUserCashAccountService cusUserCashAccountService;

    /**
     * 获取推广员数量
     * @return  Map<String,Object>
     */
    @RequestMapping("/getAgentUserCount")
    public Map<String,Object> getAgentUserCount(){
        Map<String,Object> params = new HashMap<>();
        params.put("status",3);
        BigDecimal count = agentUserService.count(params);
        return ResultUtil.SUCCESS(count);
    }

    /**
     * 校验用户推⼴员状态
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/checkAgentUser")
    public Map<String,Object> checkAgentUser(@RequestParam("userId") BigDecimal userId){
        Map<String,Object> resultMap = agentUserService.checkAgentUser(userId);
        return resultMap;
    }

    /**
     * 用户推广中心接口
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/index")
    public Map<String,Object> index(@RequestParam("userId") BigDecimal userId){
        Map<String,Object> agentUser = agentUserService.findAgentUserByUserId(userId,true,true,false);
        if(ObjectUtils.isEmpty(agentUser)){
            return ResultUtil.ERROR("未申请推广员");
        }
        Map<String,Object> resultMap = null;
        if(ObjectUtils.isNotEmpty(agentUser)){
            Integer status = (Integer)agentUser.get("status");
            if(status ==1){
                return ResultUtil.ERROR("推广员申请信息未提交");
            }
            if(status ==2){
                return ResultUtil.ERROR("推广员身份便信息审核中");
            }
            if(status ==3){//状态，正常
                resultMap = ResultUtil.SUCCESS(agentUser);
                String shareKey = MD5.getMD5(userId.toString())+"_"+userId;
                resultMap.put("shareKey",shareKey);
            }
            if(status==4){
                return ResultUtil.ERROR("推广员身份已被禁用");
            }
            if(status ==5){
                return ResultUtil.ERROR("推广员身份申请被驳回");
            }
        }
        return resultMap;
    }

    /**
     * 查询可以推广的课程数据列表
     * @param userId 用户ID
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/agentCourseList")
    public Map<String,Object> agentCourseList(@RequestParam("userId") BigDecimal userId,
                                              @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        params.put("allow",2);
        params.put("type",1);
        Map<String,Object> dataMap = courseService.findWebCoursePage(params,currentPage,10,true,false,false,true,false);
        List<Map<String,Object>> courseList = (List<Map<String,Object>>)dataMap.get("list");
        if(ObjectUtils.isNotEmpty(courseList)){
            courseList.forEach(e->{
                String shareUrl = commonConstants.get(CommonConstants.MOBILE_PATH)+"/mobile/course/info?courseId="+e.get("id")+"&shareKey="+ MD5.getMD5(userId.toString())+"_"+userId;
                e.put("shareUrl",shareUrl);
            });
        }
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 获取推广员的下级推广员列表
     * @param agentId 推广员ID
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/queryChildAgentUserList")
    public Map<String,Object> queryChildAgentUserList(@RequestParam("agentId") BigDecimal agentId,
                                                      @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String, Object> params = new HashMap<>();
        params.put("parentId", agentId);
        Map<String, Object> dataMap = agentUserService.queryAgentUserListPage(currentPage, 10, params, false, true, false, false);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 申请成为推广员
     * @param userId 用户ID
     * @param shareKey 推荐人的KEY
     * @return Map<String, Object>
     */
    @RequestMapping("/openExtension")
    public Map<String, Object> openExtension(@RequestParam("userId") BigDecimal userId,
                                             @RequestParam(value = "shareKey", required = false) String shareKey){
        return agentUserService.openExtension(userId, shareKey);
    }

    /**
     * 查询是否有可结算收益
     * @param userId 登录用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/availableSettlement")
    public Map<String,Object> availableSettlement(@RequestParam("userId") BigDecimal userId){
        Map<String,Object> agentUser = agentUserService.findAgentUserByUserId(userId,false,false,true);
        return ResultUtil.SUCCESS(agentUser);
    }


    /**
     * 查询绑定的账号
     * @param userId 登录用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/queryAgentUserAccount")
    public  Map<String,Object> queryAgentUserAccount(@RequestParam("userId") BigDecimal userId){
        TreeMap<Integer, Map<String, Object>> userCashAccount = cusUserCashAccountService.findUserCashAccount(userId);

        if(ObjectUtils.isNotEmpty(userCashAccount)){
            return ResultUtil.SUCCESS("获取绑定账户成功",userCashAccount);
        }else{
            return ResultUtil.SUCCESS("没有绑定账户",userCashAccount);
        }

    }

    /**
     * 通过账户类型，查询提现账户
     * @param userId
     * @param accountType 账户类型
     * @return Map<String,Object>
     */
    @RequestMapping("/findUserCashAccountByType")
    @ResponseBody
    public Map<String,Object> findUserCashAccountByType(@RequestParam("userId") BigDecimal userId,
                                                        @RequestParam("accountType") int accountType){
        Map<String,Object> userCashAccount = cusUserCashAccountService.findUserCashAccountByType(userId,accountType);
        return ResultUtil.SUCCESS(userCashAccount);
    }


    /**
     * 保存绑定第三方提现账号
     * @param realName 账户真实姓名
     * @param account 支付账号
     * @return Map<String,Object>
     */
    @RequestMapping("/bindAccount")
    public Map<String,Object> bindAccount(@RequestParam("userId") BigDecimal userId,
                                          @RequestParam("realName")String realName ,
                                          @RequestParam("account") String account,@RequestParam("accountType") int accountType){
        Map<String,Object> params = new HashMap<>();
        params.put("accountType",accountType);
        params.put("realName",realName);
        params.put("account",account);
        params.put("userId",userId);
        return cusUserCashAccountService.saveUserCashAccount(params);
    }
}
